<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标签" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入标签"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织编号" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入组织编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="组织名称" prop="deptName">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptName"-->
<!--          placeholder="请输入组织名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="职位类别" prop="postType">
        <el-select v-model="queryParams.postType" placeholder="请选择">
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="入党时间" prop="joinDate">
        <el-date-picker clearable size="small"
          v-model="queryParams.joinDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择入党时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="身份证" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入身份证"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择">
          <el-option
            v-for="dict in sexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学历" prop="edu">
        <el-input
          v-model="queryParams.edu"
          placeholder="请输入学历"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="年龄" prop="age">-->
<!--        <el-input-->
<!--          v-model="queryParams.age"-->
<!--          placeholder="请输入年龄"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="出生年月" prop="birthday">-->
<!--        <el-input-->
<!--          v-model="queryParams.birthday"-->
<!--          placeholder="请输入出生年月"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="管理区域" prop="areas">
        <el-input
          v-model="queryParams.areas"
          placeholder="请输入管理区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="状态" prop="status">-->
<!--        <el-select-->
<!--          v-model="queryParams.status"-->
<!--          placeholder="部门状态"-->
<!--          clearable-->
<!--          size="small"-->
<!--          style="width: 240px"-->
<!--        >-->
<!--          <el-option-->
<!--            v-for="dict in statusOptions"-->
<!--            :key="dict.dictValue"-->
<!--            :label="dict.dictLabel"-->
<!--            :value="dict.dictValue"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:cpcNumber:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:cpcNumber:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:cpcNumber:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:cpcNumber:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImportCPC"

        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cpcNumberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="id" v-if="false"/>
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="组织编号" align="center" prop="deptId" />
      <el-table-column label="组织名称" align="center" prop="deptName" width="200"/>
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="职位类别" align="center" prop="postType" :formatter="statusFormat"/>
      <el-table-column label="入党时间" align="center" prop="joinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="党员事迹简介" align="center" prop="introduction" />-->
      <el-table-column label="身份证" align="center" prop="email" width="200" />
      <el-table-column label="手机号码" align="center" prop="mobile" width="200"/>
      <el-table-column label="用户性别" align="center" prop="sex"
                       :formatter="sysSexFormat"/>
      <el-table-column label="头像地址" align="center" prop="avatar" >
        <template scope="scope">
          　　　　<img v-if="scope.row.avatar === null || scope.row.avatar=== ''"
                   src="https://i.loli.net/2021/08/27/l4u1pd7JsgTZRNx.png"
                   width="60" height="60"
                   class="head_pic"/>
          <img v-else :src="scope.row.avatar" width="60" height="60"
               class="head_pic"/>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="edu" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="出生年月" align="center" prop="birthday" width="200"/>
      <el-table-column label="管理区域" align="center" prop="areas" width="200"/>
<!--      <el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column label="标签" align="center" prop="remark" width="200"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:cpcNumber:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:cpcNumber:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改党员信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="用户编号" prop="userId">-->
<!--          <el-input v-model="form.userId" placeholder="请输入用户编号" />-->
<!--        </el-form-item>-->
        <el-form-item label="组织编号" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入组织编号" />
        </el-form-item>
<!--        <el-form-item label="组织名称" prop="deptName">-->
<!--          <el-input v-model="form.deptName" placeholder="请输入组织名称" />-->
<!--        </el-form-item>-->
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="党员头像" prop="avatar">
<!--          <el-input v-model="form.avatar" placeholder="请输入头像地址" />-->
          <el-row>
            <el-col :span="24"
            ><el-input
              v-model="form.avatar"
              placeholder="请输入网络地址"
              disabled
            /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="">
          <el-row>
            <el-col :span="12">
              &emsp;&emsp;&emsp;&emsp;&emsp;
              <el-button type="primary" @click="handleImport"
              >点击上传</el-button
              ></el-col
            >
          </el-row>
        </el-form-item>
        <el-form-item label="职位类别" prop="postType">
          <el-select v-model="form.postType" placeholder="请选择">
            <el-option
              v-for="dict in levelOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="入党时间" prop="joinDate">
          <el-date-picker clearable size="small"
            v-model="form.joinDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择入党时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="email">
          <el-input v-model="form.email" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option
              v-for="dict in sexOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学历" prop="edu">
          <el-input v-model="form.edu" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-date-picker clearable size="small"
                          v-model="form.birthday"
                          type="datetime"
                          value-format="yyyy-MM-dd"
                          placeholder="请输入出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="管理区域" prop="areas">
          <el-input v-model="form.areas" placeholder="请输入管理区域" />
        </el-form-item>
<!--        <el-form-item label="状态">-->
<!--          <el-select-->
<!--            v-model="form.status"-->
<!--            placeholder="部门状态"-->
<!--            clearable-->
<!--            size="small"-->
<!--            style="width: 240px"-->
<!--          >-->
<!--            <el-option-->
<!--              v-for="dict in statusOptions"-->
<!--              :key="dict.dictValue"-->
<!--              :label="dict.dictLabel"-->
<!--              :value="dict.dictValue"-->
<!--            />-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标志" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />-->
<!--        </el-form-item>-->
        <el-form-item label="标签" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入标签" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户上传对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color: red" slot="tip">
          提示：可上传图片,word格式的文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listCpcNumber, getCpcNumber, delCpcNumber, addCpcNumber, updateCpcNumber, exportCpcNumber } from "@/api/business/cpcNumber";
import { getToken } from '@/utils/auth'
import {
  listOssFile,
  getOssFile,
  delOssFile,
  addOssFile,
  updateOssFile,
  exportOssFile,
} from '@/api/business/ossFile'
export default {
  name: "CpcNumber",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 党员信息表格数据
      cpcNumberList: [],
      statusOptions: [],
      sexOptions: [],
      levelOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        deptId: undefined,
        deptName: undefined,
        realName: undefined,
        postType: undefined,
        joinDate: undefined,
        introduction: undefined,
        email: undefined,
        mobile: undefined,
        sex: undefined,
        avatar: undefined,
        edu: undefined,
        age: undefined,
        birthday: undefined,
        areas: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        mobile: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        realName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur"
          }
        ]
        ,
        joinDate: [
          {
            required: true,
            message: "请输入入党时间",
            trigger: "blur"
          }
        ]
        ,
        edu: [
          {
            required: true,
            message: "请输入学历",
            trigger: "blur"
          }
        ]
        ,
        areas: [
          {
            required: true,
            message: "请输入管理区域",
            trigger: "blur"
          }
        ]
        ,
        age: [
          {
            required: true,
            message: "请输入年龄",
            trigger: "blur"
          }
        ]
        ,
        deptId: [
          {
            required: true,
            message: "请输入组织编号",
            trigger: "blur"
          }
        ]
      },
      upload: {
        // 是否显示弹出层（用户上传）
        open: false,
        // 弹出层标题（用户上传）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/common/upload',
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });

    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
    this.getDicts("sys_dept_level").then(response => {
      this.levelOptions = response.data;
    });
  },
  methods: {
    /** 查询党员信息列表 */
    getList() {
      this.loading = true;
      listCpcNumber(this.queryParams).then(response => {
        this.cpcNumberList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    statusFormat(row, column) {
      if (row.menuType == "F") {
        return "";
      }
      return this.selectDictLabel(this.levelOptions, row.postType);
    },
    sysSexFormat(row, column) {
      if (row.menuType == "F") {
        return "";
      }
      return this.selectDictLabel(this.sexOptions, row.sex);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        deptId: undefined,
        deptName: undefined,
        realName: undefined,
        postType: undefined,
        joinDate: undefined,
        introduction: undefined,
        email: undefined,
        mobile: undefined,
        sex: undefined,
        avatar: undefined,
        edu: undefined,
        age: undefined,
        birthday: undefined,
        areas: undefined,
        status: "0",
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加党员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCpcNumber(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改党员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCpcNumber(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCpcNumber(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除党员信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCpcNumber(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '用户上传'
      this.upload.open = true
    },
    /** 导入按钮操作 */
    handleImportCPC() {
      this.upload.title = '用户上传'
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      console.log(response.fileName)
      console.log(response.url)
      this.form.avatar = response.url
      this.$alert(response.msg, '上传结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有党员信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCpcNumber(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
